/*
 * @Author: 霍格沃兹测试开发学社
 * @Desc: '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
 */

package com.ceshiren.aitestmini.service.impl;

import com.ceshiren.aitestmini.converter.JenkinsConverter;
import com.ceshiren.aitestmini.dto.JenkinsDTO;
import com.ceshiren.aitestmini.entity.Jenkins;
import com.ceshiren.aitestmini.service.JenkinsService;
import com.ceshiren.aitestmini.util.JobApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class JenkinsServiceImpl implements JenkinsService {
    @Autowired
    JenkinsConverter jenkinsConverter;
    @Value("${jenkins.url}")
    private String url;
    @Value("${jenkins.user}")
    private String user;
    @Value("${jenkins.pwd}")
    private String pwd;

    @Override
    public void create(JenkinsDTO jenkinsDTO) {

        Jenkins jenkins = jenkinsConverter.userDTOForUser(jenkinsDTO);
        setJenkins(jenkins);
        System.out.println(jenkins);

        JobApi.createJob(jenkins);
    }

    void setJenkins(Jenkins jenkins){
        jenkins.setUrl(url);
        jenkins.setUsername(user);
        jenkins.setPassword(pwd);
    }

    @Override
    public void build(JenkinsDTO jenkinsDTO) {

        //无参数构造
        Jenkins jenkins = jenkinsConverter.userDTOForUser(jenkinsDTO);
        setJenkins(jenkins);
        System.out.println(jenkins);

        if(jenkins.getParam()==null){
            JobApi.buildJob(jenkins);
        }else {
            //有参数构造
            JobApi.buildWithParamJob(jenkins);
        }





    }

}
